package com.anuo.app.modules.coach.controller;

import com.anuo.app.common.globalhandle.BaseController;
import com.anuo.app.common.entity.Request;
import com.anuo.app.common.persistence.PageData;
import com.anuo.app.common.utils.IdUtil;
import com.anuo.app.common.utils.JsonUtil;
import com.anuo.app.common.utils.Utils;
import com.anuo.app.modules.coach.dao.StudentDao;
import com.anuo.app.modules.coach.entity.*;
import com.anuo.app.modules.coach.service.CoachService;
import com.anuo.app.modules.coach.service.StudentService;
import com.fasterxml.jackson.core.type.TypeReference;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.sql.ResultSet;
import java.util.List;

@RestController
public class StudentController extends BaseController {
    @Autowired
    StudentService studentService;

    @PostMapping(value = "v1/apiSaveCoachComment", produces = {"application/json;charset=UTF-8"})
    public String apiSaveCoachComment(@RequestBody String encryptedReq) {
        //反序列化取得对象
        CoachComment coachCommentReq = getRequestObject(encryptedReq, new TypeReference<Request<CoachComment>>() {
        }).getFirstEntity();
        //保存评论
        coachCommentReq.setCoachid(IdUtil.getUUID());
        coachCommentReq.setCourseid(IdUtil.getUUID());
        coachCommentReq.setCommentatorid(IdUtil.getUUID());
        studentService.saveCoachComment(coachCommentReq);

        return rspSuccess();
    }

    @PostMapping(value = "v1/apiGetCoachCommentList", produces = {"application/json;charset=UTF-8"})
    public String apiGetCoachCommentList(@RequestBody String encryptedReq) {
        //反序列化取得对象
        CoachFilter coachFilter = getRequestObject(encryptedReq,
                new TypeReference<Request<CoachFilter>>() {
                }).getFirstEntity();
        PageData pageData = studentService.getCoachCommentList(coachFilter);
        return rspSuccess(pageData);
    }

    @PostMapping(value = "v1/apiSaveCoachCourseJoin", produces = {"application/json;charset=UTF-8"})
    public String apiSaveCoachCourseJoin(@RequestBody String encryptedReq) {
        CoachCoursejoin coachCoursejoin = getRequestObject(encryptedReq, new TypeReference<Request<CoachCoursejoin>>() {
        }).getFirstEntity();
        CoachCourse coachCourse = studentService.getCoachCourseById(coachCoursejoin.getCourseid());
        if (coachCourse == null) {
            return rspError("courseid 无效");
        }
        CoachStudentmoney coachStudentmoney = new CoachStudentmoney();
        coachStudentmoney.setCitycode(coachCourse.getCitycode());
        coachStudentmoney.setStudentid(coachCourse.getCreatorid());
        coachStudentmoney.setCourse_typeid(coachCourse.getCourse_type());
        boolean ishaveBalance = studentService.isHaveBalance(coachStudentmoney);
        if (!ishaveBalance) {
            return rspError("您没有可用的余额次数, 请充值");
        }
        if (studentService.isJoinState(coachCourse.getState())) {
            return rspError("课程已经开始不能报名");
        }

        if (studentService.isJoinedCourse(coachCoursejoin)) {
            return rspError("此课程您已报名, 不能再报名");
        }

        if (!studentService.isHaveEmptyPosition(coachCoursejoin)) {
            return rspError("此课程报名已满, 不能再报名");
        }

        coachCoursejoin.setCourse_typeid(coachCourse.getCourse_type());
        //减一次
        studentService.BalanceSubOne(coachCoursejoin);

        return rspSuccess();

    }

    @PostMapping(value = "v1/apiSaveCoachCourseNotJoin", produces = {"application/json;charset=UTF-8"})
    public String apiSaveCoachCourseNotJoin(@RequestBody String encryptedReq) {
        List<CoachCoursejoin> coachCoursejoinList = getRequestObject(encryptedReq, new TypeReference<Request<CoachCoursejoin>>() {
        }).getEntities();

        studentService.courseNotJoin(coachCoursejoinList);

        return rspSuccess();

    }

}

